Kotlin

Properties

Swift

Declaring Properties

                  class Address {
    var name: String = "Holmes, Sherlock"
    var street: String = "Baker"
    var city: String = "London"
    var state: String? = null
    var zip: String = "123456"
}
                
                    class Address {
    var name: String = "Holmes, Sherlock"
    var street: String = "Baker"
    var city: String = "London"
    var state: String? = nil
    var zip: String = "123456"
}

                  
                  fun copyAddress(address: Address): Address {
    val result = Address() // there's no 'new' keyword in Kotlin
    result.name = address.name // accessors are called
    result.street = address.street
    // ...
    return result
}
                
                    func copyAddress(address: Address) -> Address {
    var result = Address() // there's no 'new' keyword in Swift too
    result.name = address.name // accessors are called
    result.street = address.street
    // ...
    return result
}

                  

Getters and Setters

                  var <propertyName>[: <PropertyType>] [= <property_initializer>]
    [<getter>]
    [<setter>]
                
                    var propertyName: type { get set }

                  
                  var allByDefault: Int? // error: explicit initializer required, default getter and setter implied
var initialized = 1 // has type Int, default getter and setter
                
                    var allByDefault: Int? //default is nil
var initialized = 1 // has type Int, default getter and setter
print(allByDefault) //nil

                  
                  val simple: Int? // has type Int, default getter, must be initialized in constructor
val inferredType = 1 // has type Int and a default getter
                
                    let simple: Int? // has type Int?, no default value
let inferredType = 1 // has type Int and a default getter
print(simple) //Error: Constant 'simple' used before being initialized

                  
                  val isEmpty: Boolean
    get() = this.size == 0
                
                    var isEmpty: Bool { //'let' declarations cannot be computed properties
    get { self.count == 0 }
}

                  
                  var stringRepresentation: String
    get() = this.toString()
    set(value) {
        setDataFromString(value) // parses the string and assigns values to other properties
    }
                
                    var stringRepresentation: String {
    get { self.toString() }
    set { setDataFromString(newValue) }
}

                  
                  val isEmpty get() = this.size == 0  // has type Boolean
                
                    var isEmpty: Bool {
    get { self.count == 0 }
}

                  
                  var setterVisibility: String = "abc"
    private set // the setter is private and has the default implementation
​
var setterWithAnnotation: Any? = null
    @Inject set // annotate the setter with Inject
                
                    let setterVisibility = "abc"

                  

Backing Fields

                  var counter = 0 // Note: the initializer assigns the backing field directly
    set(value) {
        if (value >= 0) field = value
    }
                
                    var counter: Int {
    get { 0 }
    set {
        if (newValue >= 0) {
            field = newValue
        }
    }
}

                  
                  val isEmpty: Boolean
    get() = this.size == 0
                
                    var isEmpty: Bool {
    get { self.count == 0 }
}

                  

Backing Properties

                  private var _table: Map<String, Int>? = null
public val table: Map<String, Int>
    get() {
        if (_table == null) {
            _table = HashMap() // Type parameters are inferred
        }
        return _table ?: throw AssertionError("Set to null by another thread")
    }
                

Compile-Time Constants

                  const val SUBSYSTEM_DEPRECATED: String = "This subsystem is deprecated"
​
@Deprecated(SUBSYSTEM_DEPRECATED) fun foo() { ... }
                
                    static let subsystemDeprecated: String = "This subsystem is deprecated"

                  

Late-Initialized Properties and Variables

                  public class MyTest {
    lateinit var subject: TestSubject
​
    @SetUp fun setup() {
        subject = TestSubject()
    }
​
    @Test fun test() {
        subject.method()  // dereference directly
    }
}
                
                    class MyTest: XCTestCase {
    lazy var subject = TestSubject()
    
    func setUpWithError() throws {
    }
    
    func test() throws {
        subject.method()  // dereference directly
    }
}

                  

Checking whether a lateinit var is initialized (since 1.2)

                  if (foo::bar.isInitialized) {
    println(foo.bar)
}